var now = new Date(); // 当前日期
var nowDayOfWeek = now.getDay(); // 今天本周的第几天
var nowDay = now.getDate(); // 当前日
var nowMonth = now.getMonth(); // 当前月
var nowYear = now.getYear(); // 当前年
nowYear += (nowYear < 2000) ? 1900 : 0;
  
var DateUtil = {
    /**
     * 获得当前日期
     *
     * @returns
     */
    getNowDay() {
        return this.formatDate(new Date());
    },
    /**
     * 获得本周的开始时间
     *
     * @returns
     */
    getStartDayOfWeek() {
        var day = nowDayOfWeek || 7;
        return this.formatDate(new Date(now.getFullYear(), nowMonth, nowDay + 1 - day));
    },
    /**
     * 获得本周的结束时间
     *
     * @returns
     */
    getEndDayOfWeek() {
        var day = nowDayOfWeek || 7;
        return this.formatDate(new Date(now.getFullYear(), nowMonth, nowDay + 7 - day));
    },
    /**
     * 获得本月的开始时间
     *
     * @returns
     */
    getStartDayOfMonth() {
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        return this.formatDate(monthStartDate);
    },
    /**
     * 获得本月的结束时间
     *
     * @returns
     */
    getEndDayOfMonth() {
        var monthEndDate = new Date(nowYear, nowMonth, this.$getMonthDays());
        return this.formatDate(monthEndDate);
    },
    /**
     * 获得本月天数
     *
     * @returns
     */
    getMonthDays() {
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        var monthEndDate = new Date(nowYear, nowMonth + 1, 1);
        var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
    },
    /**
     * @param 日期格式化
     * @returns {String}
     */
    formatDate(date) {
        var myyear = date.getFullYear();
        var mymonth = date.getMonth() + 1;
        var myweekday = date.getDate();
        var myweekHours = date.getHours();
        var myweekMinutes = date.getMinutes();
        var myweekSeconds = date.getSeconds();
  
        if (mymonth < 10) {
            mymonth = "0" + mymonth;
        }
        if (myweekday < 10) {
            myweekday = "0" + myweekday;
        }
        if (myweekHours < 10) {
            myweekHours = "0" + myweekHours;
        }
        if (myweekMinutes < 10) {
            myweekMinutes = "0" + myweekMinutes;
        }
        if (myweekSeconds < 10) {
            myweekSeconds = "0" + myweekSeconds;
        }
        return (myyear + "-" + mymonth + "-" + myweekday+ " " + myweekHours+ ":" + myweekMinutes+ ":" + myweekSeconds);
    },


    dateRegion(num){//获取日期片段
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * num);
        var yearEnd = end.getFullYear(); //得到年份
        var monthEnd = end.getMonth() + 1;//得到月份
        var dateEnd = end.getDate();//得到日期
        var yearStart = start.getFullYear(); //得到年份
        var monthStart = start.getMonth() + 1;//得到月份
        var dateStart = start.getDate();//得到日期
        if (monthEnd < 10) monthEnd = "0" + monthEnd;
        if (dateEnd < 10) dateEnd = "0" + dateEnd;
        if (monthStart < 10) monthStart = "0" + monthStart;
        if (dateStart < 10) dateStart = "0" + dateStart;
        var timeStart = yearStart + "-" + monthStart + "-" + dateStart;    //（格式化"yyyy-MM-dd"）     
        var timeEnd = yearEnd + "-" + monthEnd + "-" + dateEnd;    //（格式化"yyyy-MM-dd"）       
        const date = [timeStart, timeEnd];
        return date
    },

    //获取两日期之间日期列表函数
    getdiffdate(stime, etime) {
        //初始化日期列表，数组
        var diffdate = new Array();
        var i = 0;
        //开始日期小于等于结束日期,并循环
        while (stime <= etime) {
            diffdate[i] = stime;
            //获取开始日期时间戳
            var stime_ts = new Date(stime).getTime();
            // console.log('当前日期：' + stime + '当前时间戳：' + stime_ts);
            //增加一天时间戳后的日期
            var next_date = stime_ts + (24 * 60 * 60 * 1000);
            //拼接年月日，这里的月份会返回（0-11），所以要+1
            var next_dates_y = new Date(next_date).getFullYear() + '-';
            var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) + '-' : (new Date(next_date).getMonth() + 1) + '-';
            var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(next_date).getDate();
            stime = next_dates_y + next_dates_m + next_dates_d;
            //增加数组key
            i++;
        }
        return diffdate
    }
};
 
export default{
    DateUtil
}
